﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;

namespace Bewise.Sharepoint.WebPartator
{
    class WebPartator
    {
        public static void Main(string[] args)
        {
            AssemblyDefinition asm = AssemblyFactory.GetAssembly(args[0]);
            WebPartInjector injector = new WebPartInjector(asm);
            
            TypeReference typeRef = asm.MainModule.Import(typeof(System.Web.UI.WebControls.WebParts.WebPart));

            List<TypeDefinition> userControls = WebType.GetUserControls(asm);

            foreach (TypeDefinition def in userControls)
            {
                TypeDefinition typeDef = new TypeDefinition(def.Name + "WebPart", def.Namespace, Mono.Cecil.TypeAttributes.Public, typeRef);
                typeDef.IsClass = true;
                typeDef.IsBeforeFieldInit = true;

                asm.MainModule.Types.Add(injector.CreateType(def));
            }

            System.IO.File.Delete(args[1]);
            AssemblyFactory.SaveAssembly(asm, args[1]);

        }
    }
}
